﻿using System;
using System.Threading;
using UnityEngine;
using Object = System.Object;

namespace DefaultNamespace
{
    public class Unity_Asset_Lock : MonoBehaviour
    {
        public static Object obj = new Object();

        private Thread t;
        
        private void Start()
        {
            Debug.Log("start ....");
            t = new Thread(Thread_with_params);
            t.Start("call thread1"); //传递参数调用多线程
            this.Thread_with_params("Main Thread ");
            
        }

        public void Thread_with_params(Object o)
        {
            lock (obj)   //给线程加锁， 一般在临界资源防止资源冲突中使用。如果用人使用就挂起，如果没人使用就占用然后执行。
            {
                Debug.Log("线程任务处理中..."+Thread.CurrentThread.ManagedThreadId);
                string s = (string) o;
                Thread.Sleep(5000);//处理流程
                Debug.Log("线程执行完毕。"+Thread.CurrentThread.ManagedThreadId);
            }
        }
    }
}